package com.db;

import java.util.UUID;

import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

@Path("/Current")
public class Current extends Controller {

    /**
     * lideca
     * 并发查询
     * 测试单条数据索引查询
     */
    public void CurrentSelect() {
        renderJson(Ret.ok("msg", Db.findById("user", 1)));
    }


    /**
     * lidecai
     * 查询没有索引的字段，模拟全表扫描的情况
     */
    public void CurrentSelectByFields() {
        renderJson(Ret.ok("msg", Db.find("select * from user where name = ?", "0b098f7e-ae1e-4592-ba15-d54ad5009fdd")));
    }

    /**
     * lideca
     * 测试高并发保存
     */
    public void CurrentSave() {
        boolean save = Db.save("user", new Record().set("name", UUID.randomUUID().toString()));
        renderJson(Ret.ok("msg", save));
    }

    /**
     * lidecai
     * 抛出异常
     */
    public void throwException() {
        System.err.println(1/0);
         renderJson(Ret.ok("msg", Db.findById("user", 1)));
    }

    /**
     * lidecai
     * 线程休眠
     */
    public void threadSleep() {
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        renderJson(Ret.ok("msg", Db.findById("user", 1)));
    }

}
